#include "bluetooth_usart.h"

// Bluetooth Buffer
USARTBuffer bluetoothInfo;
extern USARTBuffer usart_buffer; // 上位机串口缓冲区

uint8_t boolATFlag = 0; // 是否进入AT模式的标志位，默认不进入

// Bluetooth USART Struct Initialization(蓝牙串口结构体初始化)
GPIO_InitTypeDef Bluetooth_GPIO_InitStructure;   // GPIO Struct(GPIO结构体)
USART_InitTypeDef Bluetooth_USART_InitStructure; // USART Struct(串口结构体)
NVIC_InitTypeDef Bluetooth_NVIC_InitStructure;   // NVIC Struct(中断结构体)

// Bluetooth USART Initializtion(蓝牙串口初始化函数)
// TX:PA9, RX:PA10
void Bluetooth_Init(uint16_t baudRate)
{
    // Enable USART1 clock(使能蓝牙串口1和时钟)
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    // Configure USART1 pins(配置蓝牙串口1引脚)
    Bluetooth_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    Bluetooth_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PIN9
    Bluetooth_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &Bluetooth_GPIO_InitStructure);
    Bluetooth_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    Bluetooth_GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PIN10
    Bluetooth_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &Bluetooth_GPIO_InitStructure);

    // Configure parameters of USART1(配置蓝牙串口1参数)
    Bluetooth_USART_InitStructure.USART_BaudRate = baudRate;
    Bluetooth_USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    Bluetooth_USART_InitStructure.USART_StopBits = USART_StopBits_1;
    Bluetooth_USART_InitStructure.USART_Parity = USART_Parity_No;
    Bluetooth_USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    Bluetooth_USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART1, &Bluetooth_USART_InitStructure);

    // NVIC Configuration(中断配置)
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  // Open NVIC of USART1(打开蓝牙串口1的中断)
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set NVIC priority(设置NVIC分组)

    Bluetooth_NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    Bluetooth_NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    Bluetooth_NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    Bluetooth_NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&Bluetooth_NVIC_InitStructure);

    // Bluetooth Info Initialization(蓝牙信息初始化)
    bluetoothInfo.USART_TX_Index = 0;
    bluetoothInfo.USART_RX_Index = 0;
    bluetoothInfo.USART_TX_Complete = 0;
    bluetoothInfo.USART_RX_Complete = 0;
    bluetoothInfo.USART_TXFlag = 0;
    bluetoothInfo.USART_RXFlag = 0;
    bluetoothInfo.USART_TX_DataSlice = 0;
    bluetoothInfo.USART_RX_DataSlice = 0;

    USART_Cmd(USART1, ENABLE); // Enable USART1(使能串口1)
}

// Function of getting flag of receiving data
uint8_t Bluetooth_GetRXFlag(void)
{
    if (bluetoothInfo.USART_RXFlag == 1)
    {
        bluetoothInfo.USART_RXFlag = 0;
        return 1;
    }
    return 0;
}

// Bluetooth Get Send Flag(蓝牙获取发送标志函数)
uint8_t Bluetooth_GetTXFlag(void)
{
    if (bluetoothInfo.USART_TXFlag == 1)
    {
        bluetoothInfo.USART_TXFlag = 0;
        return 1;
    }
    return 0;
}

// Function of getting data slice
uint8_t Bluetooth_GetRXDataSlice(void)
{
    return bluetoothInfo.USART_RX_DataSlice;
}

// Function of Bluetooth receiving data in NVIC(蓝牙中断处理函数)
void USART1_IRQHandler(void)
{
    if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
    {
        // Read data from USART1(从蓝牙串口读取数据)
        uint8_t Bluetooth_ReceiveData = USART_ReceiveData(USART1);
        // Send data to buffer(将数据存到缓冲区)
        bluetoothInfo.USART_RX_Buffer[bluetoothInfo.USART_RX_Index++] = Bluetooth_ReceiveData;
        bluetoothInfo.USART_RX_Buffer_Slice[usart_buffer.USART_RX_Index] = Bluetooth_ReceiveData;
        bluetoothInfo.USART_RX_Index++;
        bluetoothInfo.USART_RX_DataSlice = Bluetooth_ReceiveData;
        // If buffer is full, clear it(如果缓冲区满了，清空缓冲区)
        if (Bluetooth_ReceiveData == USART_RX_End_Char || bluetoothInfo.USART_RX_Index >= (uint8_t)(USART_RX_Buffer_Length))
        {
            bluetoothInfo.USART_RX_Index = 0;
            bluetoothInfo.USART_RX_Complete = 1;
        }
        else
        {
            bluetoothInfo.USART_RXFlag = 1;
        }
        // Clear USART Buffer(清除蓝牙串口1的缓冲区)
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}

// Bluetooth Send Data(蓝牙发送数据函数)
void Bluetooth_SendData(USARTBuffer *Bluetooth_Send_Data)
{
}

// Bluetooth Receive Data(蓝牙接收数据函数)
USARTBuffer Bluetooth_ReceiveData(void)
{
    USARTBuffer temp;
    return temp;
}